package leetcode

fun main(args: Array<String>) {
    println(Long.MAX_VALUE)
    println(reverse32BitInt(1534236469))
}


fun reverse32BitInt(x: Int): Int {
    if (x == 0)
        return x
    var k = Math.abs(x)
    val numList = ArrayList<Int>()
    var m = 10
    while (k != 0) {
        numList.add(k % m)
        k /= m
    }
    println(numList)
    var r = 0L
    var decimal = 1L
    var i = 0
    while (i < numList.size && numList[i] == 0) i++

    var j = numList.size - 1
    while (j >= i) {
        r += numList[j] * decimal
        decimal *= 10
        j--
    }
    println(r)
    if (r > Int.MAX_VALUE || r < Int.MIN_VALUE)
        r = 0

    return if (x > 0) r.toInt() else (-r).toInt()
}